package com.example.woke1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.AddadressActivity;
import com.example.woke.CheckpaypswActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.diyview.Alertdialogall;
import com.woke.method.Checkpaypsw;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ZhongzhoubuyActivity extends Activity implements OnClickListener {

	private TextView mTname,mTok,mTadress,mTnamephone,mTdescrice,mTmoney,mTminmax;
	private String id,childid;
	private MyApp application;
	private String money;
	private Intent intent;
	private CheckBox mCheckBox;
	private String str0 = "",str1 = "",str2 = "",str3 = "";
	private Checkpaypsw check;
	private Alertdialogall zidingy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhongzhoubuy);
		check = new Checkpaypsw();
		zidingy = new Alertdialogall();
		intview();
	}

	private void intview() {
		application = (MyApp) getApplication();
		intent = getIntent();
		String img = intent.getStringExtra("img");
		String title = intent.getStringExtra("title");
		money = intent.getStringExtra("money");
		String content = intent.getStringExtra("content");
		id = intent.getStringExtra("id");
		childid = intent.getStringExtra("childid");
		findViewById(R.id.zhongchoubuy_image_back).setOnClickListener(this);
		mTname = (TextView) findViewById(R.id.zhongchoubuy_text_name);
		mTminmax = (TextView) findViewById(R.id.zhongchoubuy_text_minmax);
		mTmoney = (TextView) findViewById(R.id.zhongchoubuy_text_money);
		mTdescrice = (TextView) findViewById(R.id.zhongchoubuy_text_descrice);
		mTnamephone = (TextView) findViewById(R.id.zhongchoubuy_text_namephone);
		mTadress = (TextView) findViewById(R.id.zhongchoubuy_text_adress);
		mTok = (TextView) findViewById(R.id.zhongchoubuy_text_ok);
		mTnamephone.setOnClickListener(this);
		mTadress.setOnClickListener(this);
		mTok.setOnClickListener(this);
		mCheckBox = (CheckBox) findViewById(R.id.zhongchoubuy_check_box);
		mTname.setText("  "+title);
		mTmoney.setText(money+"元");
		mTdescrice.setText(content);
		mTname.setText("  "+title);
		
	}

	private void getchilddata() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "crowd");
		params.put("a", "buyCrowd");
		params.put("crowdid", id);
		params.put("user_id", application.getDatas_load().getId());
		params.put("itemid", childid);
		params.put("num", "");
		params.put("money", money);
		params.put("phone", str1);
		params.put("name", str0);
		params.put("code", str3);
		params.put("address", str2);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("发布", ""+response);
				try {
					String flag = response.getString("flag");
					String string = response.getString("info");
					if(flag.equals("success")){
						Toast.makeText(ZhongzhoubuyActivity.this, string, Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(ZhongzhoubuyActivity.this, string, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
//		
		if(requestCode == 21 && resultCode == 20){
			ArrayList<String> dalist = data.getStringArrayListExtra("result");
			str0 = dalist.get(0);//����
			str1 = dalist.get(1);//�ֻ�
			str2 = dalist.get(2);//��ַ
			str3 = dalist.get(3);//�ʱ�
			mTnamephone.setText("收货人地址："+str0+" "+str1);
			mTadress.setText(str2);
		}else if(requestCode == 700 && resultCode == 600){
			getchilddata();
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.zhongchoubuy_image_back:
			finish();
			break;
			
		case R.id.zhongchoubuy_text_ok:
			if(str0.equals("") || str1.equals("")){
				Toast.makeText(ZhongzhoubuyActivity.this, "请选择收货人信息", Toast.LENGTH_SHORT).show();
			}else if(check.check(application) == true){
				zidingy.aralogid(ZhongzhoubuyActivity.this, "还未设置支付密码","前往设置？");
			}else{
				intent.setClass(ZhongzhoubuyActivity.this, CheckpaypswActivity.class);
				intent.putExtra("paymoney", money);
				startActivityForResult(intent, 700);
//				getchilddata();
			}
			break;
			
		case R.id.zhongchoubuy_text_namephone:
			intent.setClass(ZhongzhoubuyActivity.this, AddadressActivity.class);
			intent.putExtra("frome", 1);
			startActivityForResult(intent, 21);
			break;
			
		case R.id.zhongchoubuy_text_adress:
			intent.setClass(ZhongzhoubuyActivity.this, AddadressActivity.class);
			intent.putExtra("frome", 1);
			startActivityForResult(intent, 21);
			break;

		default:
			break;
		}
	}
}
